const e = require("express");
const express = require("express");
const pool = require("../pool.js");
const router = express.Router();

router.get('/deparment',(req,res,next)=>{
  pool.query('select * from department',(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.length){
        res.send({code:200,data:result})
      }else{
        res.send({code:201,msg:'没查到'})
      }
    }
  })
})
router.post('/reg',(req,res,next)=>{
  // console.log(req.body);
  pool.query('select * from staff where staffnum=?',req.body.staffnum,(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.length){
        res.send({code:201,msg:'用户名已存在'})
      }else{
        pool.query('insert into staff set ?',req.body,(err,result_two)=>{
          if(err){
            next(err)
            return
          }else{
            if(result_two.affectedRows){
              res.send({code:200,msg:'注册成功'})
            }else{
              res.send({code:202,msg:'注册失败'})
            }
          }
        })
      }
    }
  })
})
router.post('/login',(req,res,next)=>{
  pool.query('select * from staff where staffnum=? && password=?',[req.body.staffnum,req.body.password],(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.length){
        res.send({code:200,msg:'登录成功',data:result})
      }else{
        res.send({code:201,msg:'登录失败,用户名或密码错误'})
      }
    }
  })
})
router.get('/name',(req,res,next)=>{
  // console.log(req.query);
  pool.query('select * from emp where fid=?',req.query.id,(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.length){
        res.send({code:200,msg:'查询成功',data:result[0]})
      }else{
        res.send({code:201,msg:'查询失败'})
      }
    }
  })
})
module.exports = router;